The instance of `obj` furthest from (x, y) by straight-line distance, or nothing if none are alive. If `obj` is a parent, its children count as `obj`.
Works differently on Game Boy - On the Game Boy this call looks at every object of that kind one by one and is slow when there are many (asked of a parent object, every child kind too): use it a few times a frame at most. Distances count to 1/16 of a pixel.
Works differently on Game Boy Color - On the Game Boy Color this call looks at every object of that kind one by one and is slow when there are many (asked of a parent object, every child kind too): use it a few times a frame at most. Distances count to 1/16 of a pixel.
On other consoles this differs: see Side by side and How the consoles differ.
instance_furthest(x, y, obj) -> ref<object>| Name | Type |
|---|---|
x | real |
y | real |
obj | object |
ref<object>
Looks at every live instance of an object and gives back whichever one is furthest from a point, by straight-line distance - or nothing if none are alive. It is instance_nearest turned the other way round.
x and y are a place in the room, counted from the room's top-left corner. That is the same way every instance's own x and y are counted, which is what lets the distances be compared fairly.
If two instances are exactly the same distance away, you get the one the game meets first. Which one that is depends on where the game keeps them, not on the order you made them in (see instance_nth), so do not build anything on it.
If obj is a parent, the furthest one can be any of its children. With obj_enemy as the parent of obj_slime and obj_bat, instance_furthest(x, y, obj_enemy) finds the enemy of any kind that is furthest away. The reference it gives back is typed as obj_enemy, so you can use the variables and events obj_enemy has through it.
This is a plain scan of every instance of the object, which is fine at this engine's scale (an object has tens of instances, never thousands). It measures distances exactly, however big the room is, and that makes it take a little more than twice as long as instance_nearest: about 8,000 cycles with 32 instances alive, about 3% of a frame (a frame is about 281,000 cycles).
An instance destroyed earlier in the same frame is still found until the end of the frame. So "find the furthest, destroy it, find the furthest again" gives you the same one every time. To destroy several, go through them with with instead (see instance_destroy).
var far = instance_furthest(x, y, obj_doc_flag);
if (far != nothing) {
direction = dir_to(x, y, far.x, far.y);
speed = 1;
}A scout that heads for the flag that is furthest from it, to check the far corner of the room first. Use dist_to too if you want to know how far that is.
The mirror image of instance_nearest, with the same shape of answer: a plain linear scan of every live instance of `obj` and of each of its descendants (the parent first), fine at this engine’s scale. If two are exactly as far away, the one the scan meets first wins - and that order is not the order you made them in (see instance_nth), so do not build on which one it is. It still returns an instance you destroyed earlier in this same frame, so "furthest, destroy, furthest again" hands you the same one forever - see instance_destroy for the sweep that works instead. Cost: it measures every distance exactly, however far apart they are, which takes a little more than twice as long as instance_nearest - about 8,000 cycles with 32 instances of obj alive, 3 percent of a frame (about 281,000 cycles).